import 'package:get/get.dart';
import 'package:xvideos/hj_page/find/rank_module/rank_main_logic.dart';
import 'package:xvideos/hj_utils/api_service/acg_service.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../hj_model/home/video_list_resp.dart';
import '../../../hj_utils/api_service/find_service.dart';

class RankSubPageLogic extends GetxController {
  final RankType pageType;
  final int sortType; //1 -日榜, 2 -周榜, 3 -月榜, 3 -年榜
  bool get isACG => pageType == RankType.comic || pageType == RankType.cartoon;
  RefreshController refreshCtr = RefreshController();
  int currentPage = 1;
  List<dynamic>? dataList;

  RankSubPageLogic({
    required this.pageType,
    this.sortType = 1,
  });

  @override
  void onReady() {
    super.onReady();
    loadData();
  }

  loadData({int pageNum = 1}) async {
    dataList ??= [];
    if (isACG) {
      final resp = await ACGService.acgRanking(
        pageNum,
        10,
        mediaType: pageType.param,
        type: sortType,
      );
      if (resp != null) {
        currentPage = pageNum;
        if (pageNum == 1) dataList?.clear();
        dataList?.addAll(resp.list ?? []);
        resp.hasNext == true ? refreshCtr.loadComplete() : refreshCtr.loadNoData();
        refreshCtr.refreshCompleted();
      }else {
        refreshCtr.refreshCompleted();
        refreshCtr.loadComplete();
      }
    } else {
      VideoListResp? res = await FindService.vidRanking(
        pageNum,
        10,
        newsType: pageType.param,
        type: sortType,
      );
      if (res != null) {
        currentPage = pageNum;
        if (pageNum == 1) dataList?.clear();
        dataList?.addAll(res.videos ?? []);
        res.hasNext == true ? refreshCtr.loadComplete() : refreshCtr.loadNoData();
        refreshCtr.refreshCompleted();
      }else {
        refreshCtr.refreshCompleted();
        refreshCtr.loadComplete();
      }
    }
    update();
  }

  loadMoreData() => loadData(pageNum: currentPage + 1);
}
